package com.lxl.message;

import com.infobright.logging.Log4jEtlLogger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.pentaho.di.trans.steps.infobrightoutput.KettleEtlLogger;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class XmlMsgProvider implements MessageProvider {
    private static DateFormat msgDataFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    private Log4jEtlLogger logger = new Log4jEtlLogger(this.getClass());

    @Override
    public String provideMessage(MsgStatus status, String msgContent) {
        Document msgDoc = null;
        switch (status){
            case ERROR:
            case REQUEST_ERROR:
            case FILE_ERROR:
            case INTERNAL_SERVER_ERROR:{
                //传入字符串
                msgDoc = createMsgEntity(createHeader(status.getIndex()), createBody("object",msgContent));
                break;
            }
            case LONG_TASK_RUN:{
                msgDoc = createMsgEntity(createHeader(status.getIndex()), msgContent);
                break;
            }
            case SUCCESS:{
                //传入xml格式的字符串
                //msgContent = StringEscapeUtils.unescapeXml(msgContent);
                msgDoc = createMsgEntity(createHeader(status.getIndex()), createBody(msgContent));
                break;
            }
            default:{
                msgDoc = DocumentHelper.createDocument();
                break;
            }
        }
        return msgDoc.asXML();
    }

    @Override
    public String provideMessage(MsgStatus status, String msgContent, File templateFile) {
        String msg = null;

        try {
            //抽象
            String templateStr = FileUtils.readFileToString(templateFile);
            TemplateMeta templateMeta = TemplateParser.buildTemplateMetaList(templateStr)[1];

            if (templateMeta != null){
                TemplateData templateData = TemplateParser.buildTemplateData(status, msgContent, templateMeta);
                TemplateParser.parse(templateData, templateMeta.structure, 0, templateData.arrayLength);
                msg = TemplateParser.buildResult(templateData, templateMeta, "xml");
            }else{
                msg = provideMessage(status, msgContent);
            }
        } catch (IOException e) {
            System.out.println("XML template file init failed.");
            e.printStackTrace();
        }

        return msg;
    }

    protected Element createHeader(int code){
        Element headerElement = DocumentHelper.createElement("header");
        headerElement.addElement("msgCode").addText(code + "");
        headerElement.addElement("msgId").addText(UUID.randomUUID()+"");
        headerElement.addElement("createTime").addText(msgDataFormat.format(new Date()));
        return headerElement;
    }

    protected Element createBody(String tag, String content){
        return DocumentHelper.createElement(tag).addText(content);
    }

    protected Element createBody(String xmlStr){
        Element bodyElement = null;
        try {
            bodyElement = DocumentHelper.parseText(xmlStr).getRootElement();
        } catch (DocumentException e) {
            System.out.println("Xml parse fail.");
            e.printStackTrace();
        }
        return bodyElement;
    }

    protected Document createMsgEntity(Element header, Element body){
        Element root = DocumentHelper.createElement("root");
        Document document = DocumentHelper.createDocument(root);
        root.add(header);
        root.addElement("body").add(body);
        return document;
    }

    protected Document createMsgEntity(Element header, String content){
        Element root = DocumentHelper.createElement("root");
        Document document = DocumentHelper.createDocument(root);
        root.add(header);
        root.addElement("body").addText(content);
        return document;
    }

}
